package com.kexun.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class QQUtils {
    public static JSONObject getUserInfo(String code) {
        String accessToken = getAccessToken(code);
        log.info("accessToken:" + accessToken);
        String openId = getOpenId(accessToken);
        log.info("openId:" + openId);
        return getUserInfo(accessToken, openId);
    }

    public static String getAccessToken(String code) {
        String s = HttpUtils.doGet("https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101953860&client_secret=6d454bce4c15b203eea5f8840051819a&code=" + code + "&redirect_uri=http://www.kexunsoftware.com/qqlogin&fmt=json");
        JSONObject resJson = JSON.parseObject(s);
        return resJson.getString("access_token");
    }

    public static String getOpenId(String token) {
        String s = HttpUtils.doGet("https://graph.qq.com/oauth2.0/me?access_token=" + token);
        s = s.substring(s.indexOf("(") + 1, s.lastIndexOf(")"));
        JSONObject resJson = JSON.parseObject(s);
        return resJson.getString("openid");
    }


    public static JSONObject getUserInfo(String token, String openId) {
        String s = HttpUtils.doGet("https://graph.qq.com/user/get_user_info?access_token=" + token + "&oauth_consumer_key=101953860&openid=" + openId);
        JSONObject parseObject = JSON.parseObject(s);
        parseObject.put("openid", openId);
        return parseObject;
    }

    public static void main(String[] args) {
//        String code = "A968E04D9F9E1354F695117A803DF1AC";
//        String s = HttpUtils.doGet("https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101953860&client_secret=6d454bce4c15b203eea5f8840051819a&code=" + code + "&redirect_uri=http://www.kexunsoftware.com/qqlogin&fmt=json");
//        System.out.println("QQ登录返回" + s);
//        {"access_token":"70AC24700885D400DA7B7EFFDF1F1DB7","expires_in":"7776000","refresh_token":"D0E4EC205DD736F4E2F8F14B8AF38FA3"}
//
//        String s = HttpUtils.doGet("https://graph.qq.com/oauth2.0/me?access_token=70AC24700885D400DA7B7EFFDF1F1DB7");
//        System.out.println(s);
        //callback( {"client_id":"101953860","openid":"4FF680AB6B45F84DA9C57F5E4FB37974"} );
//        String s1 = HttpUtils.doGet("https://graph.qq.com/user/get_user_info?access_token=70AC24700885D400DA7B7EFFDF1F1DB7&oauth_consumer_key=101953860&openid=4FF680AB6B45F84DA9C57F5E4FB37974");
//        System.out.println(s1);
//        {
//            "ret": 0,
//                "msg": "",
//                "is_lost":0,
//                "nickname": "始梦少年.",
//                "gender": "男",
//                "gender_type": 1,
//                "province": "湖南",
//                "city": "邵阳",
//                "year": "1996",
//                "constellation": "",
//                "figureurl": "http:\/\/qzapp.qlogo.cn\/qzapp\/101953860\/4FF680AB6B45F84DA9C57F5E4FB37974\/30",
//                "figureurl_1": "http:\/\/qzapp.qlogo.cn\/qzapp\/101953860\/4FF680AB6B45F84DA9C57F5E4FB37974\/50",
//                "figureurl_2": "http:\/\/qzapp.qlogo.cn\/qzapp\/101953860\/4FF680AB6B45F84DA9C57F5E4FB37974\/100",
//                "figureurl_qq_1": "http://thirdqq.qlogo.cn/g?b=oidb&k=A8Iib2g0LWrWMg5iazf2Xo0w&s=40&t=1597546540",
//                "figureurl_qq_2": "http://thirdqq.qlogo.cn/g?b=oidb&k=A8Iib2g0LWrWMg5iazf2Xo0w&s=100&t=1597546540",
//                "figureurl_qq": "http://thirdqq.qlogo.cn/g?b=oidb&k=A8Iib2g0LWrWMg5iazf2Xo0w&s=640&t=1597546540",
//                "figureurl_type": "1",
//                "is_yellow_vip": "0",
//                "vip": "0",
//                "yellow_vip_level": "0",
//                "level": "0",
//                "is_yellow_year_vip": "0"
//        }
    }
}
